<hr>
<p>title: 2006.count-number-of-pairs-with-absolute-difference-k<br>date: 2021-12-06 15:32:26<br>mathjax: true<br>tags:</p>
<pre><code>- LeeCode
</code></pre><p>categories: </p>
<pre><code>- LeeCode
</code></pre><p>hidden: true</p>
<h2 id="cateHidden-false"><a href="#cateHidden-false" class="headerlink" title="cateHidden: false"></a>cateHidden: false</h2><h3 id="描述"><a href="#描述" class="headerlink" title="描述"></a>描述</h3><blockquote>
<p>Given an integer array nums and an integer k, return the number of pairs (i, j) where i &lt; j such that |nums[i] - nums[j]| == k.</p>
<p>The value of |x| is defined as:</p>
<ul>
<li>x if x &gt;= 0.</li>
<li>-x if x &lt; 0.</li>
</ul>
</blockquote>
<h3 id="测试用例"><a href="#测试用例" class="headerlink" title="测试用例"></a>测试用例</h3><pre><code class="lang-bash">Input: nums = [1,2,2,1], k = 1
Output: 4
Explanation: The pairs with an absolute difference of 1 are:
- [1,2,2,1]
- [1,2,2,1]
- [1,2,2,1]
- [1,2,2,1]
</code></pre>
<h3 id="题解"><a href="#题解" class="headerlink" title="题解"></a>题解</h3><p>暴力双重循环。</p>
<pre><code class="lang-js">/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
var countKDifference = function(nums, k) {
    let sum = 0, i = 0, j = 1;
    for(let i = 0; i &lt; nums.length - 1; i++) {
        for(let j = i + 1; j &lt; nums.length; j++) {
            if(Math.abs(nums[i] - nums[j]) === k) {
                sum++;
            }
        }
    }
    return sum;  
};
</code></pre>
<h3 id="结果"><a href="#结果" class="headerlink" title="结果"></a>结果</h3><blockquote>
<p>Accepted</p>
<p>237/237 cases passed (80 ms)</p>
<p>Your runtime beats 77.23 % of javascript submissions</p>
<p>Your memory usage beats 23.12 % of javascript submissions (40.7 MB)</p>
</blockquote>
